package cn.itcast.service.impl;

import cn.itcast.dao.MenuDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.exception.BusinessException;
import cn.itcast.pojo.ErrorResult;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;


    //条件查询菜单
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据条件分页查询检查项信息
        Page<Menu> page = menuDao.findAll();
        //构建分页数据返回
        for (Menu menu : page) {
            menu = saveChildren(menu);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }
    private Menu saveChildren(Menu menu) {
        List<Menu> chil = menuDao.findByParentId(menu.getId());
        if (chil==null|| chil.size()==0){
            return menu;
        }
        for (Menu menu1 : chil) {
            saveChildren(menu1);
        }
        menu.setChildren(chil);
        return menu;
    }
    @Override
    public PageResult findAll() {
        return null;
    }
    //根据id查询菜单
    @Override
    public Menu findOneById(Integer id) {
        return menuDao.findOneById(id);
    }

    @Override
    public void insert(Menu menu, Integer[] permissionIds) {

                menuDao.insert(menu);
                Integer id = menu.getId();
                List<Integer> list = menuDao.findPermissionIds(id);
                if (id!=null){
                    List<Integer> ids = new ArrayList<>(Arrays.asList(permissionIds));
                    ids.removeAll(list);
                    Integer[] arr = new Integer[ids.size()];
                    for (int i = 0; i < ids.size(); i++) {
                        arr[i] = ids.get(i);
                    }
                    if (arr.length>0){
                        menuDao.insertPermission(id,arr);
                    }
                }else{
                    throw new BusinessException(ErrorResult.fail());
                }


    }

    @Override
    public void update(Menu menu, Integer[] permissionIds) {
           menuDao.update(menu);
           //根据id删除所有权限
           menuDao.removeAllPermission(menu.getId());
           if (permissionIds.length>0){
               menuDao.insertPermission(menu.getId(),permissionIds);
           }
    }
    @Transactional
    @Override
    public void deleteById(Integer id) {
        deleteChild(id);
        menuDao.removeAllPermission(id);
        menuDao.removeAllRole(id);
        menuDao.deleteById(id);
    }

    private void deleteChild(Integer id){
        List<Menu> menus = menuDao.findById(id);
        if (menus==null||menus.size()==0){
            return;
        }
        for (Menu menu : menus) {
            deleteChild(menu.getId());
            menuDao.removeAllPermission(menu.getId());
            menuDao.removeAllRole(menu.getId());
            menuDao.deleteById(menu.getId());
        }

    }


}
